package web;

import pojo.po.Role;
import pojo.po.User;
import pojo.vo.AddUser;
import pojo.vo.PageBean;
import pojo.vo.Result;
import server.UserServerImpl;
import utils.BaseController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/Servlet/*")
public class Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int i = request.getRequestURI().lastIndexOf("/");
        String substring = request.getRequestURI().substring(i + 1);
        try {
            this.getClass().getDeclaredMethod(substring, HttpServletRequest.class, HttpServletResponse.class).invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    private void getUserList(HttpServletRequest request, HttpServletResponse response) {
        try {
            PageBean pageBean = BaseController.parseJSON2Object(request, PageBean.class);
            UserServerImpl userServer = new UserServerImpl();
            Result userList = userServer.getUserList(pageBean);
            BaseController.printResult(response, userList);
        } catch (IOException e) {
            try {
                BaseController.printResult(response, new Result(false, "执行失败"));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * 删除指令用户
     *
     * @param request
     * @param response
     */
    private void delete(HttpServletRequest request, HttpServletResponse response) {
        try {
            UserServerImpl userServer = new UserServerImpl();
            userServer.delete(request.getParameter("id"));
            BaseController.printResult(response, new Result(true, "删除成功", null, null));
        } catch (IOException e) {
            try {
                BaseController.printResult(response, new Result(false, "删除失败"));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * 查询权限列表
     */
    void getRoleList(HttpServletRequest request, HttpServletResponse response) {
        try {
            UserServerImpl userServer = new UserServerImpl();
            List<Role> rolse = userServer.getRoleList();
            BaseController.printResult(response, new Result(true, "查询成功", rolse, null));
        } catch (IOException e) {
            try {
                BaseController.printResult(response, new Result(false, "删除失败"));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * 新增用户
     */
    void handleCreateConfirm(HttpServletRequest request, HttpServletResponse response) {
        try {
            UserServerImpl userServer = new UserServerImpl();
            AddUser addUser = BaseController.parseJSON2Object(request, AddUser.class);
            userServer.addUser(addUser);
            BaseController.printResult(response,new Result(true,"添加成功",null,null));
        } catch (IOException e) {
            try {
                BaseController.printResult(response,new Result(false,"添加失败"));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * 修改用户
     */
    void updata(HttpServletRequest request, HttpServletResponse response){
        try {
            UserServerImpl userServer = new UserServerImpl();
            AddUser addUser = BaseController.parseJSON2Object(request, AddUser.class);
            userServer.updata(addUser);
            BaseController.printResult(response,new Result(true,"添加成功",null,null));
        } catch (IOException e) {
            try {
                BaseController.printResult(response,new Result(false,"添加失败"));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}
